package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.OrgDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.OrgEntity;
import eteam.aps.systeminterface.model.reqparam.IdParam;
import eteam.aps.systeminterface.service.IOrgController;
import eteam.aps.systemservice.bll.OrgBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 机构服务
 */
@Api(tags = "机构服务控制器")
@RestController
//@RequestMapping(value = "/api/org")
public class OrgController implements IOrgController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrgBll orgBll;

    /**
     * 获取机构树[全机构树]
     *
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取机构树[全机构树]")
    @Override
    @GetMapping(value = "/api/org/getorgtree")
    public R<List<OrgEntity>> getOrgTree() throws Exception, AuthException {
        return R.success(orgBll.getOrgTree());
    }

    /**
     * 根据机构ID获取下级机构列表
     *
     * @param orgId: 机构Id
     * @return
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据机构ID获取下级机构列表")
    @ApiImplicitParam(name = "orgId", value = "机构ID(为空获取顶级机构)")
    @Override
    @GetMapping(value = "/api/org/getorglist")
    public R<List<OrgEntity>> getOrgList(@RequestParam String orgId) throws Exception, AuthException {
        return R.success(orgBll.getOrgList(orgId));
    }

    /**
     * 搜索机构列表
     *
     * @param keyword: 检索关键字
     * @return
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "搜索机构列表")
    @ApiImplicitParam(name = "keyword", value = "搜索关键字")
    @Override
    @GetMapping(value = "/api/org/searchorglist")
    public R<List<OrgEntity>> searchOrgList(@RequestParam String keyword) throws Exception, AuthException {
        return R.success(orgBll.searchOrgList(keyword));
    }

    /**
     * 新增机构
     *
     * @param orgDto: 机构信息
     * @return
     */
    @ApiOperationSupport(order = 4, ignoreParameters = {"orgDto.forgid"})
    @ApiOperation(value = "新增机构")
    @Override
    @PostMapping(value = "/api/org/createorg")
    public R<OrgEntity> createOrg(@Validated(Insert.class) @RequestBody OrgDto orgDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        return R.success(orgBll.createOrg(tokenDto, orgDto));
    }

    /**
     * 更新机构
     *
     * @param orgDto: 机构信息
     * @return
     */
    @ApiOperationSupport(order = 5, ignoreParameters = {})
    @ApiOperation(value = "更新机构")
    @Override
    @PostMapping(value = "/api/org/updateorg")
    public R<OrgEntity> updateOrg(@Validated(Update.class) @RequestBody OrgDto orgDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        return R.success(orgBll.updateOrg(tokenDto, orgDto));
    }

    /**
     * 删除机构
     *
     * @param orgParam: 机构Id参数
     * @return
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除机构")
    @Override
    @PostMapping(value = "/api/org/deleteorg")
    public R deleteOrg(@Validated @RequestBody IdParam orgParam) throws Exception, AuthException {
        orgBll.deleteOrg(orgParam.getObjid());
        return R.success();
    }
}
